﻿#include "../../ctlImageButton.h"

namespace swui
{
	bool ctlImageButton::create(ctlRoot* parent, ctlRect rect)
	{
		if(parent == 0 || isCreated() || !parent->isCreated()) return false; 
		std::lock_guard<decltype(_eventTrans)> gurad(
			_eventTrans.getControlEventTrans(parent));
		_control_virtual_class_end = this;
		_basic_create_(rect, parent);
		_controlType = ctlControlType::ctlImageButton;
		setImage(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff);
		_image_button_alpha.disenable 
			= _image_button_alpha.down
			= _image_button_alpha.stay
			= _image_button_alpha.leave = 0xff;
		_image_button_judge_alpha = 0;
		_image_selected_alpha = ctlQuadColor<unsigned char>(0xff, 0xff, 0xff, 0xff);
		_image_selected_index = ctlQuadColor<unsigned>(0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff);
		_image_selected_enable = false;
		_image_button_scale = false;
		_image_button_is_mask = false;
		redraw();
		return true;
	}
	void ctlImageButton::setImage(unsigned leave, unsigned stay,
		unsigned down, unsigned disenabled)
	{
		ctlQuadColor<unsigned> qc;
		qc.disenable = disenabled;
		qc.down = down;
		qc.leave = leave;
		qc.stay = stay;
		setImage(qc);
	}
	void ctlImageButton::setImageAlpha(unsigned char leave, unsigned char stay, 
		unsigned char down, unsigned char disenabled)
	{
		ctlQuadColor<unsigned char> qc;
		qc.disenable = disenabled;
		qc.down = down;
		qc.leave = leave;
		qc.stay = stay;
		setImageAlpha(qc);
	}
	void ctlImageButton::setImage(ctlQuadColor<unsigned> index)
	{
		_image_button_index = index;
		redraw();
	}
	ctlQuadColor<unsigned> ctlImageButton::getImageIndex()
	{
		return _image_button_index;
	}
	ctlQuadColor<std::shared_ptr<ctlImage>> ctlImageButton::getImage()
	{
		ctlQuadColor<std::shared_ptr<ctlImage>> qc;
		qc.disenable = getDisenabledImage();
		qc.down = getDownImage();
		qc.leave = getLeaveImage();
		qc.stay = getStayImage();
		return qc;
	}
	void ctlImageButton::setLeaveImage(unsigned index)
	{
		_image_button_index.leave = index;
		redraw();
	}
	void ctlImageButton::setStayImage(unsigned index)
	{
		_image_button_index.stay = index;
		redraw();
	}
	void ctlImageButton::setDownImage(unsigned index)
	{
		_image_button_index.down = index;
		redraw();
	}
	void ctlImageButton::setDisenabledImage(unsigned index)
	{
		_image_button_index.disenable = index;
		redraw();
	}
	std::shared_ptr<ctlImage> ctlImageButton::getLeaveImage()
	{
		return getImageQueue()->getElement(_image_button_index.leave);
	}
	std::shared_ptr<ctlImage> ctlImageButton::getStayImage()
	{
		return getImageQueue()->getElement(_image_button_index.stay);
	}
	std::shared_ptr<ctlImage> ctlImageButton::getDownImage()
	{
		return getImageQueue()->getElement(_image_button_index.down);
	}
	std::shared_ptr<ctlImage> ctlImageButton::getDisenabledImage()
	{
		return getImageQueue()->getElement(_image_button_index.disenable);
	}
	void ctlImageButton::setMaskColor(ctlColor mask)
	{
		_image_button_mask_color = mask;
		redraw();
	}
	ctlColor ctlImageButton::getMaskColor()
	{
		return _image_button_mask_color;
	}
	bool ctlImageButton::hasMask()
	{
		return _image_button_is_mask;
	}
	void ctlImageButton::enableMask(bool value)
	{
		_image_button_is_mask = value;
		redraw();
	}
	void ctlImageButton::setJudgeAlpha(unsigned char c)
	{
		_image_button_judge_alpha = c;
		redraw();
	}
	unsigned char ctlImageButton::getJudgeAlpha()
	{
		return _image_button_judge_alpha;
	}
	void ctlImageButton::setImageAlpha(ctlQuadColor<unsigned char> a)
	{
		_image_button_alpha = a;
		redraw();
	}
	ctlQuadColor<unsigned char> ctlImageButton::getImageAlpha()
	{
		return _image_button_alpha;
	}
	bool ctlImageButton::isScaleImage()
	{
		return _image_button_scale;
	}
	void ctlImageButton::setScaleImage(bool b)
	{
		_image_button_scale = b;
		redraw();
	}
	void ctlImageButton::enableSelected(bool e)
	{
		_image_selected_enable = e;
		_image_is_selected = false;
		redraw();
	}
	bool ctlImageButton::isSelectedEnabled()
	{
		return _image_selected_enable;
	}
	void ctlImageButton::setSelectedImage(ctlQuadColor<unsigned> index)
	{
		_image_selected_index = index;
		redraw();
	}
	ctlQuadColor<std::shared_ptr<ctlImage>> ctlImageButton::getSelectedImage()
	{
		ctlQuadColor<unsigned> ix = getSelectedImageIndex();
		std::shared_ptr<ctlImageQueue> qe = getImageQueue();
		return ctlQuadColor<std::shared_ptr<ctlImage>>(
			qe->getElement(ix.leave), qe->getElement(ix.stay), 
			qe->getElement(ix.down), qe->getElement(ix.disenable));
	}
	ctlQuadColor<unsigned> ctlImageButton::getSelectedImageIndex()
	{
		return _image_selected_index;
	}
	void ctlImageButton::setSelectedImageAlpha(ctlQuadColor<unsigned char> e)
	{
		_image_selected_alpha = e;
		redraw();
	}
	ctlQuadColor<unsigned char> ctlImageButton::getSelectedImageAlpha()
	{
		return _image_selected_alpha;
	}
	void ctlImageButton::_mouse_event_(ctlMouseEvent* me)
	{
		if(me->eventModel != ctlMouseEventModel::cut) return;
		IDrag::_mouse_event_(me);
		if(me->eventType  == ctlMouseEventType::leftPress)
			_image_is_selected = !_image_is_selected;
		redraw();
	}
	bool ctlImageButton::isSelected()
	{
		return _image_is_selected;
	}
	void ctlImageButton::setSelected(bool w)
	{
		_image_is_selected = w;
		redraw();
	}
}